package com.example.springbootrbac.controller;

import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.pojo.Result;
import com.example.springbootrbac.pojo.ResultCode;
import com.example.springbootrbac.service.UpdateService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class UpdateController {
    @Resource(name = "updateServiceImpl")
    UpdateService updateService;

    @PostMapping("/index/announcement/update")
    public Result<String> updateAnnouncement(AnnouncementEntity announcement,HttpServletRequest request){
        System.out.println("前端参数:"+announcement);
        if (ObjectUtils.isEmpty(announcement.getTitle()) || ObjectUtils.isEmpty(announcement.getText())){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"标题或正文不能为空哦");
        }
        String token = updateService.updateAnnouncement(announcement,request);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"好像出了点小问题，请稍后尝试吧");
        }
        return Result.success("公告修改成功",token);
    }
    @PostMapping("/index/room/update")
    public Result<String> updateRoom(RoomEntity room, HttpServletRequest request){
        System.out.println("前端参数:"+room);
        if (ObjectUtils.isEmpty(room)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"不接受空参数");
        }else if(room.getAvailable() < 0 || room.getOccupied() < 0){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"可入住或已入住户数不能小于0");
        }
        String token = updateService.updateRoom(room,request);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"您无权修改此记录");
        }
        return Result.success("房屋修改成功",token);
    }
    @PostMapping("/index/messageBoards/update")
    public Result<String> updateMessages(MessagesBoardEntity messagesBoard, HttpServletRequest request){
        System.out.println("前端参数:"+messagesBoard);
        if (ObjectUtils.isEmpty(messagesBoard)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"不接受空参数");
        }
        String token = updateService.updateMessageBoard(messagesBoard,request);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"您无权修改此记录");
        }
        return Result.success("留言修改成功",token);
    }
    @PostMapping("/index/course/update")
    public Result<String> updateBuilding(CourseEntity course, HttpServletRequest request){
        System.out.println("前端参数:"+course);
        if (ObjectUtils.isEmpty(course)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"不接受空参数");
        }else if(course.getLength() < 0 ){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"课时不能小于0");
        }
        String token = updateService.updateCourse(course,request);
        if (ObjectUtils.isEmpty(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_ERROR,"您无权修改此记录");
        }else if("FK_ERROR_1".equals(token)){
            return Result.failure(ResultCode.FAILURE_UPDATE_FOREIGN_KEY_ERROR,"不存在此教室");
        }else if("FK_ERROR_2".equals(token)){
            return  Result.failure(ResultCode.FAILURE_UPDATE_FOREIGN_KEY_ERROR,"不存在此任课教师");
        }
        return Result.success("课程修改成功",token);
    }
}
